﻿//#define BlueStacks

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;

namespace GameControler
{
    public static class GameHelper
    {

#if BlueStacks 

        public const string GameName = "BlueStacks App Play";

         public static Point ReturnButtonPoint
        {
            get { return new Point(1371, 131); }
        }

          public static uint HitDefaultButton
        {
            get { return NativeHelper.MakeLong(1366, 795); }
        }

        public static uint LeftButton
        {
            get { return NativeHelper.MakeLong(121, 671); }
        }

        public static uint RightButton
        {
            get { return NativeHelper.MakeLong(382, 676); }
        }

        public static uint TopButton
        {
            get { return NativeHelper.MakeLong(218, 523); }
        }

        public static uint BottomButton
        {
            get { return NativeHelper.MakeLong(240, 820); }
        }

        public static uint ReturnHomeButton
        {
            get { return NativeHelper.MakeLong((short)1258, (short)675); }
        }

#else
        public const string GameName = "HelloCpp";

        public static uint LeftButton
        {
            get { return NativeHelper.MakeLong(96, 533); }
        }

        public static uint RightButton
        {
            get { return NativeHelper.MakeLong(284, 519); }
        }

        public static uint TopButton
        {
            get { return NativeHelper.MakeLong(178, 418); }
        }

        public static uint BottomButton
        {
            get { return NativeHelper.MakeLong(191, 623); }
        }
        public static uint ReturnHomeButton
        {
            get { return NativeHelper.MakeLong(1217, 667); }
        }

        public static uint HitDefaultButton
        {
            get { return NativeHelper.MakeLong(1030, 571); }
        }

#endif

        public static bool IsGetHandle = false;

        public static string MainFloder = "GameImages";

        public static uint ReturnButton
        {
            get { return NativeHelper.MakeLong((short)ReturnButtonPoint.X, (short)ReturnButtonPoint.Y); }
        }

        public static Point ReturnButtonPoint
        {
            get { return new Point(1111, 70); }
        }

        public static uint GiveButton
        {
            get { return NativeHelper.MakeLong(733, 596); }
        }

        public static uint SetButton
        {
            get { return NativeHelper.MakeLong(745, 757); }
        }


        public static uint StartButton
        {
            get { return NativeHelper.MakeLong(1299, 736); }
        }

        public static uint EnterButton
        {
            get { return NativeHelper.MakeLong(754, 197); }
        }

        public static uint OpenButton
        {
            get { return NativeHelper.MakeLong(1020, 616); }
        }


        #region 副本操作

        public static uint Hit1Button
        {
            get { return NativeHelper.MakeLong(990, 792); }
        }

        public static uint Hit2Button
        {
            get { return NativeHelper.MakeLong(1136, 795); }
        }

        public static uint Hit3Button
        {
            get { return NativeHelper.MakeLong(1182, 614); }
        }

        public static uint Hit4Button
        {
            get { return NativeHelper.MakeLong(1367, 572); }
        }

        public static uint HitOtherButton
        {
            get { return NativeHelper.MakeLong(1361, 425); }
        }

        #endregion

        public static void SendClick(this IntPtr handle, Point point)
        {
            //NativeHelper.TrackerWindow(handle);
            TrackerWind(handle, point);

            uint value = NativeHelper.MakeLong((short)point.X, (short)point.Y);
            SendClick(handle, value);
        }

        public static void SendClick(this IntPtr handle, uint button)
        {
            //NativeHelper.TrackerWindow(handle);

            NativeHelper.PostMessage(handle, NativeHelper.WM_LBUTTONDOWN, NativeHelper.MK_LBUTTON, button);
            NativeHelper.PostMessage(handle, NativeHelper.WM_LBUTTONUP, NativeHelper.MK_LBUTTON, button);
        }

        public static void SendMove(this IntPtr handle, uint button, double time = 100)
        {
            Thread t = new Thread((ParameterizedThreadStart)delegate
            {
                NativeHelper.PostMessage(handle, NativeHelper.WM_LBUTTONDOWN, NativeHelper.MK_LBUTTON, button);
                Thread.Sleep((int)time);
                NativeHelper.PostMessage(handle, NativeHelper.WM_LBUTTONUP, NativeHelper.MK_LBUTTON, button);
            });
            t.Start();
        }

        public static void SendMove(this IntPtr handle, Point point, double time = 100)
        {
            uint value = NativeHelper.MakeLong((short)point.X, (short)point.Y);
            Thread t = new Thread((ParameterizedThreadStart)delegate
            {
                NativeHelper.PostMessage(handle, NativeHelper.WM_LBUTTONDOWN, NativeHelper.MK_LBUTTON, value);
                Thread.Sleep((int)time);
                NativeHelper.PostMessage(handle, NativeHelper.WM_LBUTTONUP, NativeHelper.MK_LBUTTON, value);
            });
            t.Start();
        }


        public static void SendDown(this IntPtr handle, Point point)
        {
            uint value = NativeHelper.MakeLong((short)point.X, (short)point.Y);
            NativeHelper.PostMessage(handle, NativeHelper.WM_LBUTTONDOWN, NativeHelper.MK_LBUTTON, value);
        }

        public static void SendUp(this IntPtr handle, Point point)
        {
            uint value = NativeHelper.MakeLong((short)point.X, (short)point.Y);
            NativeHelper.PostMessage(handle, NativeHelper.WM_LBUTTONUP, NativeHelper.MK_LBUTTON, value);
        }

        public static void SendDown(this IntPtr handle, uint button)
        {
            NativeHelper.PostMessage(handle, NativeHelper.WM_LBUTTONDOWN, NativeHelper.MK_LBUTTON, button);
        }

        public static void SendUp(this IntPtr handle, uint button)
        {
            NativeHelper.PostMessage(handle, NativeHelper.WM_LBUTTONUP, NativeHelper.MK_LBUTTON, button);
        }

        static System.Windows.Window window = new System.Windows.Window()
        {
            WindowState = System.Windows.WindowState.Normal,
            WindowStyle = System.Windows.WindowStyle.None,
            Background = System.Windows.Media.Brushes.Red,
            Width = 50,
            Height = 50
        };

        public static void TrackerWind(IntPtr AHandle, Point point)
        {
            return;
            if (NativeHelper.IsWindow(AHandle))
            {
                NativeRECT vRect = new NativeRECT();
                if (GetWindowRect(AHandle, ref vRect))
                {
                    window.Left = vRect.left + point.X;
                    window.Top = vRect.top + point.Y;

                    window.Show();
                }
            }
        }


        [StructLayout(LayoutKind.Sequential)]
        struct NativeRECT
        {
            public int left;
            public int top;
            public int right;
            public int bottom;
        }

        [Flags]
        enum MouseEventFlag : uint
        {
            Move = 0x0001,
            LeftDown = 0x0002,
            LeftUp = 0x0004,
            RightDown = 0x0008,
            RightUp = 0x0010,
            MiddleDown = 0x0020,
            MiddleUp = 0x0040,
            XDown = 0x0080,
            XUp = 0x0100,
            Wheel = 0x0800,
            VirtualDesk = 0x4000,
            Absolute = 0x8000
        }

        [DllImport("user32.dll")]
        static extern bool SetCursorPos(int X, int Y);

        [DllImport("user32.dll")]
        static extern void mouse_event(MouseEventFlag flags, int dx, int dy, uint data, UIntPtr extraInfo);

        [DllImport("user32.dll")]
        static extern IntPtr FindWindow(string strClass, string strWindow);

        [DllImport("user32.dll")]
        static extern IntPtr FindWindowEx(IntPtr hwndParent, HandleRef hwndChildAfter, string strClass, string strWindow);

        [DllImport("user32.dll")]
        static extern bool GetWindowRect(IntPtr hwnd, ref NativeRECT rect);

    }
}
